﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace Gmantis.Controls.Chart
{
    public class PlotArea : DependencyObject
    {
        // Fields
        public static readonly DependencyProperty BackgroundProperty = DependencyProperty.Register("Background", typeof(Brush), typeof(PlotArea), new PropertyMetadata(null));
        public static readonly DependencyProperty ColumnProperty = DependencyProperty.Register("Column", typeof(int), typeof(PlotArea), new PropertyMetadata(0));
        private Rect plotRect = new Rect();
        private Rectangle rect;
        public static readonly DependencyProperty RowProperty = DependencyProperty.Register("Row", typeof(int), typeof(PlotArea), new PropertyMetadata(0));
        public static readonly DependencyProperty StrokeProperty = DependencyProperty.Register("Stroke", typeof(Brush), typeof(PlotArea), new PropertyMetadata(null));
        public static readonly DependencyProperty StrokeThicknessProperty = DependencyProperty.Register("StrokeThickness", typeof(double), typeof(PlotArea), new PropertyMetadata(1.0));

        // Methods
        internal void SetPlotX(double x, double w)
        {
            this.plotRect.X = x;
            this.plotRect.Width = w;
        }

        internal void SetPlotY(double y, double h)
        {
            this.plotRect.Y = y;
            this.plotRect.Height = h;
        }

        // Properties
        public Brush Background
        {
            get
            {
                return (Brush)base.GetValue(BackgroundProperty);
            }
            set
            {
                base.SetValue(BackgroundProperty, value);
            }
        }

        public int Column
        {
            get
            {
                return (int)base.GetValue(ColumnProperty);
            }
            set
            {
                base.SetValue(ColumnProperty, value);
            }
        }

        public int Row
        {
            get
            {
                return (int)base.GetValue(RowProperty);
            }
            set
            {
                base.SetValue(RowProperty, value);
            }
        }

        public Brush Stroke
        {
            get
            {
                return (Brush)base.GetValue(StrokeProperty);
            }
            set
            {
                base.SetValue(StrokeProperty, value);
            }
        }

        public double StrokeThickness
        {
            get
            {
                return (double)base.GetValue(StrokeThicknessProperty);
            }
            set
            {
                base.SetValue(StrokeThicknessProperty, value);
            }
        }

        internal UIElement UIElement
        {
            get
            {
                if (((this.plotRect.Width <= 0.0) || (this.plotRect.Height <= 0.0)) || ((this.Background == null) && (this.Stroke == null)))
                {
                    return null;
                }
                if (this.rect == null)
                {
                    this.rect = new Rectangle();
                }
                this.rect.Fill = this.Background;
                this.rect.Stroke = this.Stroke;
                this.rect.StrokeThickness = this.StrokeThickness;
                this.rect.Width = this.plotRect.Width;
                this.rect.Height = this.plotRect.Height;
                Canvas.SetLeft(this.rect, this.plotRect.X);
                Canvas.SetTop(this.rect, this.plotRect.Y);
                return this.rect;
            }
        }
    }
}
